package hlhj.fhp.checkpoint.atys

import android.Manifest
import android.annotation.SuppressLint
import android.view.View
import com.tbruyelle.rxpermissions.RxPermissions
import hlhj.fhp.checkpoint.BaseAty
import hlhj.fhp.checkpoint.R
import hlhj.fhp.checkpoint.iview.VipView
import hlhj.fhp.checkpoint.javaBean.MyVipBean
import hlhj.fhp.checkpoint.presenter.VipPresenter
import kotlinx.android.synthetic.main.lo_pub_head.*
import com.yzq.zxinglibrary.android.CaptureActivity
import android.content.Intent
import com.example.mymvp.log
import com.yzq.zxinglibrary.common.Constant
import hlhj.fhp.checkpoint.javaBean.MyBaseBean
import kotlinx.android.synthetic.main.aty_vip.*
import android.support.v4.graphics.TypefaceCompat
import android.graphics.Typeface
import android.widget.Toast
import hlhj.fhp.checkpoint.utils.ToastUtils


/**
 * Created by Never Fear   on 2018\12\14 0014.
Never More....
 */
class VipAty:BaseAty<VipView,VipPresenter>(), VipView {
    override fun initView() {
        tvTittle.setText("尊享会员")
        btRightIcon.setImageResource(R.drawable.bt_scan)
        btRightIcon.visibility=View.VISIBLE
    }
    override fun setMyVip(myVipBean: MyVipBean) {

        if (myVipBean.info==null){
            loCard.visibility=View.GONE
        }else{
            loCard.visibility=View.VISIBLE
            tvPrice.text="￥${myVipBean.info.card_price.split(".")[0]}"
        }


    }

    override fun setBindVip(myBaseBean: MyBaseBean) {
        presenter?.getVip(this)
        ToastUtils.toast("欢迎您，尊敬的VIP会员!",this)
    }

    override fun logicStart() {
        presenter?.getVip(this)
    }

    private val REQUEST_CODE_SCAN: Int=2345

    override fun setListener() {
        btRightIcon.setOnClickListener {
            if (!RxPermissions.getInstance(this)
                    .isGranted(Manifest.permission.CAMERA)||!RxPermissions.getInstance(this).isGranted(Manifest.permission.READ_EXTERNAL_STORAGE)){
                RxPermissions.getInstance(this).request(Manifest.permission.CAMERA,Manifest.permission.READ_EXTERNAL_STORAGE)
                        .subscribe {
                            b->
                            if (b){
                                val intent = Intent(this, CaptureActivity::class.java)
                                startActivityForResult(intent, REQUEST_CODE_SCAN)
                            }
                        }
            }else{
                val intent = Intent(this, CaptureActivity::class.java)
                startActivityForResult(intent, REQUEST_CODE_SCAN)
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode!=-1)return
        when(requestCode){
            REQUEST_CODE_SCAN->{
                if (data != null) {
                    val content = data.getStringExtra(Constant.CODED_CONTENT)
                    log("fhp","扫描结果为${content}")
                    presenter?.bindVip(content,this)

                }
            }
        }
    }
    override fun bindView(): VipView {
        return this
    }

    override fun createPresenter(): VipPresenter {
        return VipPresenter()
    }

    override val contentId: Int= R.layout.aty_vip
}